const moment = require('moment/moment');

const avatarImages = [
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/27bb543f1131fb98a17fbdbd143440a5.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/3232175b81a1ab78100bbc0fdf24ab85.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/3244c43bcdb27c40636c14378a5e7ddc.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/418773f45eeb49834780203711e87862.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/44bfe123445e838e3aaee1da8aa77462.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/5f293c7c2250d145bc3bd780d08b21e9.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/506363a9fc3ffc253abad0746ea57da4.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/6729801bc0cefd298e2a166fbc112352.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/757b7c9b751c601d05f1a961d90cef11.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/7a7b54099ec0294665b94bffed999ee8.png',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/8877e1eea8868266b49a95e662cf3b74.png',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/8d4ac8789160c481b6abd518b1f41a50.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/a0bef5e11f96498d3fa1c3ef2362f145.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/d2a7cae4a44800a9%21400x400.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/e51e81083b2589d1c8d22e8506c8dee1.png',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/f2d9a017ec996ffe%21400x400.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/f459b5a8cb42b7d117dc6082daedbaaa.jpg',
  'https://chauge-job.oss-cn-hangzhou.aliyuncs.com/avatar/fb0a5a2d57fa947594de7885dfa797e9.jpg',
];

module.exports = class extends think.Model {
  async getJobInfo(data) {
    const jobList = data;
    for (const item of jobList) {
      if (!item.cate_id || item.cate_id === '0') {
        item.cate_name = '不限行业';
      } else {
        const cate = item.cate_id.split(',')[0];
        const cateInfo = await this.model('post_category')
          .where({ id: cate })
          .find();
        item.cate_name = cateInfo.name;
      }

      if (!item.company) {
        item.company_num = false;
      } else {
        item.company_num = item.company.split(',').length;
      }

      // const interCount = await this.model('process')
      //   .alias('a')
      //   .join({
      //     user_list: {
      //       join: 'left',
      //       as: 'u',
      //       on: ['user_id', 'id'],
      //     },
      //   })
      //   .field('a.*, u.avatar')
      //   .where({ 'a.job_id': item.id, 'u.avatar': ['!=', null] })
      //   .select();

      // item.avatars = this.setAvatar(item.interview, interCount);

      // if (item.city_name) {
      //   const citys = item.city_name.split(',');
      //   if (citys.length && citys.length > 3) {
      //     item.citys = citys.slice(0, 3).join('、') + '等';
      //   } else if (citys.length && citys.length <= 3) {
      //     item.citys = citys.join('、');
      //   }
      // }

      item.create_info = moment(item.create_time).format('MM-DD') + '发布';
    }
    return jobList;
  }
  setAvatar(interviewCount, interviewInfo) {
    const arr = [];
    let hasMore = false;
    let num = interviewCount;

    if (interviewCount > 5) {
      hasMore = true;
    }

    if (interviewInfo.length === 0) {
      for (const itemAvatar of avatarImages.sort(this.randomSort)) {
        if (arr.length < 5 && arr.length < interviewCount) {
          arr.push(itemAvatar);
        }
      }
    } else {
      if (interviewInfo.length < interviewCount) {
        for (const itemAvatar of interviewInfo) {
          if (arr.length < 5) {
            arr.push(itemAvatar.avatar);
          } else {
            break;
          }
        }
        for (const itemAvatar of avatarImages.sort(this.randomSort)) {
          if (arr.length < 5) {
            arr.push(itemAvatar);
          } else {
            break;
          }
        }
      } else {
        num = interviewInfo.length;
        for (const itemAvatar of interviewInfo) {
          if (arr.length < 5) {
            arr.push(itemAvatar.avatar);
          } else {
            break;
          }
        }
      }
    }
    return {
      arr: arr,
      hasMore: hasMore,
      num: num,
    };
  }
  randomSort(a, b) {
    return Math.random() > 0.5 ? -1 : 1;
  }
};
